<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>游戏管理</el-breadcrumb-item>
      <el-breadcrumb-item>游戏列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div>
        <el-row :gutter="20">
          <el-col :span="8">
            <!-- 搜索框 -->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getList">
              <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addDialogVisible = true;">发布新文档</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="guidelineList"
          border
          stripe
        >
          <el-table-column type="index"></el-table-column>
          <el-table-column
            prop="title"
            label="标题"
          >
          </el-table-column>
          <el-table-column
            prop="toWho"
            label="发布对象"
          >
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="查看与修改文档" placement="top" :enterable="false">
                <el-button type="primary" icon="el-icon-edit" size="mini"
                           @click="fillUpdateForm(scope.row);updateDialogVisible = true"></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除文档" placement="top" :enterable="false">
                <el-button type="danger" icon="el-icon-delete" size="mini"
                           @click="deleteGuideline(scope.row.id)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 15]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        <el-dialog
          title="发布文档"
          :visible.sync="addDialogVisible"
          width="80%">
          <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="120px">
            <el-form-item prop="title" label="文档标题">
              <el-input v-model="addForm.title" clearable></el-input>
            </el-form-item>
            <el-form-item prop="toWho" label="文档发布对象">
              <el-select v-model="addForm.toWho" placeholder="请选择发布角色">
                <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.roleName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <mavon-editor v-model="addForm.content" ref="md" @imgAdd="$addImg"/>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addGuideline">确 定</el-button>
          </span>
        </el-dialog>
        <el-dialog
          title="修改文档"
          :visible.sync="updateDialogVisible"
          width="80%">
          <el-form ref="updateFormRef" :model="updateForm" :rules="updateFormRules" label-width="120px">
            <el-form-item prop="title" label="文档标题">
              <el-input v-model="updateForm.title" clearable></el-input>
            </el-form-item>
            <el-form-item prop="toWho" label="文档发布对象">
              <el-select v-model="updateForm.toWho" placeholder="请选择发布角色">
                <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.roleName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <mavon-editor v-model="updateForm.content" @imgAdd="$addImg" ref="md"/>
          <span slot="footer" class="dialog-footer">
            <el-button @click="updateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateGuideline">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>

  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        query: ''
      },
      total: 0,
      addDialogVisible: false,
      updateDialogVisible: false,
      updateFormRules: {
        title: [
          {
            required: true,
            message: '请输入文档标题',
            trigger: 'blur'
          }
        ]
      },
      updateForm: {
        id: 0,
        content: '',
        title: '',
        toWho: ''
      },
      guidelineList: [],
      addForm: {
        title: '',
        content: '',
        toWho: 3
      },
      addFormRules: {
        title: [
          {
            required: true,
            message: '请输入文档标题',
            trigger: 'blur'
          }
        ]
      },
      roleList: []
    }
  },
  created () {
    this.getList()
    this.getRoleList()
  },
  methods: {
    async getList () {
      const { data: res } = await this.$http.get('/guideline/all', { params: this.queryInfo })
      if (res.code !== 200) {
        this.$message.error(res.message)
        return
      }
      console.log(res)
      this.total = res.data.guidelines.total
      this.guidelineList = res.data.guidelines.records
    },
    async getRoleList () {
      const { data: res } = await this.$http.get('/role/all')
      if (res.code === 200) {
        this.roleList = res.data.roles
      } else {
        this.$message.error('请求文档列表失败')
      }
    },
    addGuideline () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/guideline', this.addForm)
        if (res.code === 201) {
          this.$message.success('添加文档成功')
          this.addDialogVisible = false
          await this.getList()
        } else {
          console.log(res)
          this.$message.error('添加文档失败')
        }
      })
    },
    deleteGuideline (guidelineId) {
      this.$confirm('此操作将永久删除该文档, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('/guideline', { data: { id: guidelineId } })
        if (res.code === 200) {
          this.$message.success('删除文档成功')
          await this.getList()
        } else {
          this.$message.error('删除文档失败')
          console.log(res)
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    updateGuideline () {
      this.$confirm('此操作将修改该文档, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.put('/guideline', this.updateForm)
        if (res.code === 200) {
          this.$message.success('修改文档成功')
          this.updateDialogVisible = false
          await this.getList()
        } else {
          console.log(res)
          this.$message.error('修改文档失败')
        }
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getList()
    },
    fillUpdateForm (info) {
      this.updateForm.title = info.title
      this.updateForm.id = info.id
      this.updateForm.toWho = info.toWho
      this.updateForm.content = info.content
    },
    $addImg (pos, $file) {
      // 第一步.将图片上传到服务器.
      const formData = new FormData()
      formData.append('file', $file)
      this.$http({
        url: '/api/document/upload/uploadGameSource',
        method: 'post',
        data: formData,
        headers: { 'Content-Type': 'multipart/form-data' }
      }).then((res) => {
        const _res = res.data.data
        this.$refs.md.$img2Url(pos, this.$http.defaults.baseURL + _res.url)
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
